package com.yanqu.road.server.cmd.banquet.cross.goodbanquet;

import com.yanqu.road.entity.banquet.BanquetInfo;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.pb.banquet.CrossBanquetProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.banquet.BanquetConfigMgr;
import com.yanqu.road.server.manger.banquet.BanquetMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

/**
 * @Description 开启百福宴会 跨服返回
 * @Author cwq
 * @Data 2021/11/9 14:34
 */
@Cmd(code = Protocol.S_GOOD_BANQUET_OPEN_FROM_CROSS, desc = "开启百福宴 跨服返回区服 协议")
public class GoodBanquetOpenFromCrossCmd extends AbstractCommand {
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        CrossBanquetProto.GoodBanquetOpenToGameServerMsg crossMsg = CrossBanquetProto.GoodBanquetOpenToGameServerMsg.parseFrom(packet.getBytes());
        CrossBanquetProto.OpenCrossGoodBanquetResp.Builder stcMsg = CrossBanquetProto.OpenCrossGoodBanquetResp.newBuilder();
        stcMsg.setRet(crossMsg.getRet());
        // 取消预扣
        BanquetInfo banquetInfo = BanquetConfigMgr.getBanquetInfo(crossMsg.getType());
        Property property = null;
        if(banquetInfo != null){
            property = PropertyHelper.parseStringToProperty(banquetInfo.getOpenConsume());
            player.getModule(PreReduceModule.class).restorePreReduce(property);
        }
        if(crossMsg.getRet() == 0){
            player.getModule(CurrencyModule.class).removeCurrency(property, eLogMoneyType.Banquet, eLogMoneyType.BanquetOpenGoodBanquetConsume);
            BanquetMgr.addBanquetConductTimes(player);
            stcMsg.setEndTime(crossMsg.getEndTime());
        }
        player.sendPacket(Protocol.U_CROSS_GOOD_BANQUET_OPEN, stcMsg);
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
